package com.lowca.news.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "t_category", catalog = "com_lowca_news")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Category extends IdEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	private String name;
	private String description;
	private Integer level;
	private Category parent;
	private Integer sortValue;
	private List<Category> children = new ArrayList<Category>(0);
	private Set<Article> articles = new HashSet<Article>(0);

	public Category() {
		super();
	}

	public Category(String name, String description, Integer level,
			Category parent, Integer sortValue, List<Category> children,
			Set<Article> articles) {
		super();
		this.name = name;
		this.description = description;
		this.level = level;
		this.parent = parent;
		this.sortValue = sortValue;
		this.children = children;
		this.articles = articles;
	}

	@Column(name = "name", length = 30)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "level", length = 4)
	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "parent_id")
	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "parent")
	@OrderBy("sortValue ASC")
	public List<Category> getChildren() {
		return children;
	}

	public void setChildren(List<Category> children) {
		this.children = children;
	}

	@Column(name = "sort_value", length = 4)
	public Integer getSortValue() {
		return sortValue;
	}

	public void setSortValue(Integer sortValue) {
		this.sortValue = sortValue;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "category")
	public Set<Article> getArticles() {
		return articles;
	}

	public void setArticles(Set<Article> articles) {
		this.articles = articles;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public boolean equals(Object other) {
		if (this == other)
			return true;
		if (!(other instanceof Article))
			return false;
		final Category category = (Category) other;
		if (!getName().equals(category.getName()))
			return false;
		if (!getLevel().equals(category.getLevel()))
			return false;
		if (getParent() != null && !getParent().equals(category.getParent()))
			return false;
		return true;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + getName().hashCode();
		result = prime * result + getLevel().hashCode();
		result = prime * result
				+ ((getParent() == null) ? 0 : getParent().hashCode());
		return result;
	}
}
